//
//  CustomAlertView.swift
//  d11
//
//  Created by Wai Yin Chan on 3/10/2017.
//  Copyright © 2017 Tech_001. All rights reserved.
//

import Foundation
import UIKit

class CsAlertView: UIView, Modal {
    var backgroundView = UIView()
    var dialogView = UIView()
    
    
    var closeBtn: UIButton?
    
    var htmlBtn: UIButton?
    var  html5Btn : UIButton?
    var appBtn: UIButton?
    var appsBtn: UIButton?
    var numberBtn: UIButton?
    var numbersBtn: UIButton?
    
    
    var htmlImageView: UIView?
    var appImageView: UIView?
    convenience init(title:String,number:String) {
        self.init(frame: UIScreen.main.bounds)
        backgroundView.backgroundColor = UIColor.clear
        backgroundView.alpha = 0
        backgroundView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTappedOnBackgroundView)))
        addSubview(backgroundView)
        backgroundView.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview()
            make.top.equalTo(0)
        }
        
        dialogView  = UIView.init(frame: UIScreen.main.bounds)
        dialogView.backgroundColor = UIColor.init(white: 1, alpha: 1)
        dialogView.layer.cornerRadius = 5;
        dialogView.layer.masksToBounds = true
        addSubview(dialogView)
        dialogView.snp.makeConstraints { (make) in
            make.width.equalTo(300)
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(257.5)
        }
        
        let  titleBtn : UIButton  = UIButton.init(type: .custom)
        titleBtn.setBackgroundImage(imageNamed("icon_common_pop_window_title_bg"), for: .normal)
        titleBtn.setTitle(title, for: .normal)
        titleBtn.adjustsImageWhenHighlighted = false
        titleBtn.titleEdgeInsets = UIEdgeInsets(top: -10,left: 0, bottom: 0, right: 0)
        dialogView.addSubview(titleBtn)
        titleBtn.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalTo(59)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        closeBtn  = UIButton.init(type: .custom)
        closeBtn?.setImage(imageNamed("icon_common_pop_window_close"), for: .normal)
        dialogView.addSubview(closeBtn!)
        closeBtn?.snp.makeConstraints { (make) in
            make.width.equalTo(27)
            make.height.equalTo(27)
            make.right.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        htmlBtn  = UIButton.init(type: .custom)
        htmlBtn?.setImage(imageNamed("icon_common_pop_window_self_help"), for: .normal)
        htmlBtn?.adjustsImageWhenHighlighted = false
        dialogView.addSubview(htmlBtn!)
        let btnW = 300/3
        
        htmlBtn?.snp.makeConstraints { (make) in
            make.width.equalTo(btnW)
            make.height.equalTo(btnW)
            make.left.equalToSuperview()
            make.top.equalTo(titleBtn.snp.bottom).offset(5)
        }
        
        appBtn  = UIButton.init(type: .custom)
        appBtn?.setImage(imageNamed("icon_common_pop_window_manual"), for: .normal)
        appBtn?.adjustsImageWhenHighlighted = false
        dialogView.addSubview(appBtn!)
        
        appBtn?.snp.makeConstraints { (make) in
            make.width.equalTo(btnW)
            make.height.equalTo(btnW)
            make.centerX.equalToSuperview()
            make.top.equalTo(titleBtn.snp.bottom).offset(5)
        }
        
        
        
        numberBtn  = UIButton.init(type: .custom)
        numberBtn?.setImage(imageNamed("icon_common_pop_window_phone"), for: .normal)
        numberBtn?.adjustsImageWhenHighlighted = false
        dialogView.addSubview(numberBtn!)
        
        numberBtn?.snp.makeConstraints { (make) in
            make.width.equalTo(btnW)
            make.height.equalTo(btnW)
            make.right.equalToSuperview()
            make.top.equalTo(titleBtn.snp.bottom).offset(5)
        }
        
        
        html5Btn  = UIButton.init(type: .custom)
        html5Btn?.setTitle("自助客服", for: .normal)
        html5Btn?.layer.borderColor = UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 1).cgColor
        html5Btn?.layer.cornerRadius = 5
        html5Btn?.layer.borderWidth = 0
        html5Btn?.adjustsImageWhenHighlighted = false
        html5Btn?.layer.masksToBounds = true
        html5Btn?.setBackgroundImage(imageNamed("icon_btn_bg"), for:.normal)
        html5Btn?.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        html5Btn?.addTarget(self, action: #selector(buttonAction(sender:event:)), for: .allTouchEvents)
        dialogView.addSubview(html5Btn!)
        html5Btn?.snp.makeConstraints { (make) in
            make.width.equalTo(btnW-10)
            make.height.equalTo(40)
            make.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-25)
        }
        
        appsBtn  = UIButton.init(type: .custom)
        appsBtn?.setTitle("人工客服", for: .normal)
        appsBtn?.setBackgroundImage(imageNamed("icon_btn_bg"), for:.normal)
        appsBtn?.layer.borderColor = UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 1).cgColor
        appsBtn?.layer.borderWidth = 0
        appsBtn?.layer.cornerRadius = 5
        appsBtn?.adjustsImageWhenHighlighted = false
        appsBtn?.layer.masksToBounds = true
        appsBtn?.setTitleColor(UIColor.white, for: .normal)
          appsBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        appsBtn?.addTarget(self, action: #selector(buttonAction(sender:event:)), for: .allTouchEvents)
        dialogView.addSubview(appsBtn!)
        appsBtn?.snp.makeConstraints { (make) in
            make.width.equalTo(btnW-10)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-25)
        }
        
        numbersBtn  = UIButton.init(type: .custom)
        numbersBtn?.setTitle("电话回拨", for: .normal)
        numbersBtn?.setBackgroundImage(imageNamed("icon_btn_bg"), for:.normal)
        numbersBtn?.layer.borderColor = UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 1).cgColor
        numbersBtn?.layer.borderWidth = 0
        numbersBtn?.layer.cornerRadius = 5
        numbersBtn?.adjustsImageWhenHighlighted = false
        numbersBtn?.layer.masksToBounds = true
        numbersBtn?.setTitleColor(UIColor.white, for: .normal)
        numbersBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        numbersBtn?.addTarget(self, action: #selector(buttonAction(sender:event:)), for: .allTouchEvents)
        dialogView.addSubview(numbersBtn!)
        numbersBtn?.snp.makeConstraints { (make) in
            make.width.equalTo(btnW-10)
            make.height.equalTo(40)
            make.right.equalToSuperview().offset(-10);
            make.bottom.equalToSuperview().offset(-25)
        }
    }
    convenience init(title:String) {
        self.init(frame: UIScreen.main.bounds)
        backgroundView.backgroundColor = UIColor.clear
        backgroundView.alpha = 0
        backgroundView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTappedOnBackgroundView)))
        addSubview(backgroundView)
        backgroundView.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview()
            make.top.equalTo(0)
        }
        
        dialogView  = UIView.init(frame: UIScreen.main.bounds)
        dialogView.backgroundColor = UIColor.init(white: 1, alpha: 1)
        dialogView.layer.cornerRadius = 5;
        dialogView.layer.masksToBounds = true
        addSubview(dialogView)
        dialogView.snp.makeConstraints { (make) in
            make.width.equalTo(280)
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(287.5)
        }
        
        let  titleBtn : UIButton  = UIButton.init(type: .custom)
        titleBtn.setBackgroundImage(imageNamed("icon_common_pop_window_title_bg"), for: .normal)
        titleBtn.setTitle(title, for: .normal)
        titleBtn.adjustsImageWhenHighlighted = false
        titleBtn.titleEdgeInsets = UIEdgeInsets(top: -10,left: 0, bottom: 0, right: 0)
        dialogView.addSubview(titleBtn)
        titleBtn.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalTo(59)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        closeBtn  = UIButton.init(type: .custom)
        closeBtn?.setImage(imageNamed("icon_common_pop_window_close"), for: .normal)
        dialogView.addSubview(closeBtn!)
        closeBtn?.snp.makeConstraints { (make) in
            make.width.equalTo(27)
            make.height.equalTo(27)
            make.right.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        htmlBtn  = UIButton.init(type: .custom)
        htmlBtn?.setImage(imageNamed("icon_common_pop_window_self_help"), for: .normal)
        htmlBtn?.adjustsImageWhenHighlighted = false
        dialogView.addSubview(htmlBtn!)
        let btnW = 280/2
        
        htmlBtn?.snp.makeConstraints { (make) in
            make.width.equalTo(btnW)
            make.height.equalTo(btnW)
            make.left.equalToSuperview()
            make.top.equalTo(titleBtn.snp.bottom).offset(5)
        }
        
        appBtn  = UIButton.init(type: .custom)
        appBtn?.setImage(imageNamed("icon_common_pop_window_manual"), for: .normal)
        appBtn?.adjustsImageWhenHighlighted = false
        dialogView.addSubview(appBtn!)
        
        appBtn?.snp.makeConstraints { (make) in
            make.width.equalTo(btnW)
            make.height.equalTo(btnW)
            make.right.equalToSuperview()
            make.top.equalTo(titleBtn.snp.bottom).offset(5)
        }
        
        
        html5Btn  = UIButton.init(type: .custom)
        html5Btn?.setTitle("自助客服", for: .normal)
        html5Btn?.layer.borderColor = UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 1).cgColor
        html5Btn?.layer.cornerRadius = 5
        html5Btn?.layer.borderWidth = 0
        html5Btn?.adjustsImageWhenHighlighted = false
        html5Btn?.layer.masksToBounds = true
        html5Btn?.setBackgroundImage(imageNamed("icon_btn_bg"), for:.normal)
        html5Btn?.addTarget(self, action: #selector(buttonAction(sender:event:)), for: .allTouchEvents)
        dialogView.addSubview(html5Btn!)
        html5Btn?.snp.makeConstraints { (make) in
            make.width.equalTo(btnW-20)
            make.height.equalTo(40)
            make.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-25)
        }
        
        appsBtn  = UIButton.init(type: .custom)
        appsBtn?.setTitle("人工客服", for: .normal)
        appsBtn?.setBackgroundImage(imageNamed("icon_btn_bg"), for:.normal)
        appsBtn?.layer.borderColor = UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 1).cgColor
        appsBtn?.layer.borderWidth = 0
        appsBtn?.layer.cornerRadius = 5
        appsBtn?.adjustsImageWhenHighlighted = false
        appsBtn?.layer.masksToBounds = true
        appsBtn?.setTitleColor(UIColor.white, for: .normal)
        appsBtn?.addTarget(self, action: #selector(buttonAction(sender:event:)), for: .allTouchEvents)
        dialogView.addSubview(appsBtn!)
        appsBtn?.snp.makeConstraints { (make) in
            make.width.equalTo(btnW-20)
            make.height.equalTo(40)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(-25)
        }
    }
    
    convenience init(title:String, buttons:[UIButton]?) {
        self.init(frame: UIScreen.main.bounds)
        
        backgroundView.frame = frame
        backgroundView.backgroundColor = UIColor.clear
        backgroundView.alpha = 0
        addSubview(backgroundView)
        
        
        let dialogViewWidth = frame.width-64
        
        let titleLabel = UILabel(frame: CGRect(x: 4, y: 8, width: dialogViewWidth - 8, height: 28))
        titleLabel.font = UIFont(name: titleLabel.font.fontName, size: CGFloat(15))
        titleLabel.text = title
        titleLabel.textAlignment = .center
        dialogView.addSubview(titleLabel)
        let separatorLineView = UIView()
        separatorLineView.frame.origin = CGPoint(x: 16, y: titleLabel.frame.height + 8)
        separatorLineView.frame.size = CGSize(width: dialogViewWidth - 32, height: 0.5)
        separatorLineView.backgroundColor = UIColor.init(white: 0.85, alpha: 1)
        dialogView.addSubview(separatorLineView)
        
        let imageStack = TZStackView()
        imageStack.axis = .horizontal
        imageStack.distribution = .fillEqually
        imageStack.spacing = 80
        imageStack.frame.origin = CGPoint(x: 40, y: separatorLineView.frame.height + separatorLineView.frame.origin.y - 6)
        imageStack.frame.size = CGSize(width: dialogViewWidth - 80 , height: 124)
        let image1 = UIImageView(image: UIImage(named: "heper_self"))
        let image2 = UIImageView(image: UIImage(named: "atificial"))
    
        image1.contentMode = .scaleAspectFit
        image2.contentMode = .scaleAspectFit
        
        imageStack.addArrangedSubview(image1)
        imageStack.addArrangedSubview(image2)
        
        image1.anchors.heightAnchor.constraintEqualToConstant(c: image1.frame.width).isActive = true
        image2.anchors.heightAnchor.constraintEqualToConstant(c: image2.frame.width).isActive = true
        
        imageStack.frame.size = CGSize(width: dialogViewWidth - 80 , height: image1.frame.height - 36)
        dialogView.addSubview(imageStack)
        
        htmlImageView = image1
        appImageView = image2
        
        let buttonStack = TZStackView()
        if buttons != nil {
            buttonStack.axis = .horizontal
            buttonStack.distribution = .fillEqually
            buttonStack.frame.origin = CGPoint(x: 0, y: titleLabel.frame.height + 8 + separatorLineView.frame.height + 8 + imageStack.frame.height)
            buttonStack.frame.size = CGSize(width: frame.width - 64, height: 40)
            
            for button in buttons! {
                buttonStack.addArrangedSubview(button)
            }
            dialogView.addSubview(buttonStack)
        }else{
            buttonStack.frame.size = CGSize(width: 0, height: 0)
        }
        
        let dialogViewHeight = titleLabel.frame.height + 8 + separatorLineView.frame.height + 8 + imageStack.frame.height + buttonStack.frame.height
        dialogView.frame.origin = CGPoint(x: 32, y: frame.height)
        dialogView.frame.size = CGSize(width: frame.width-64, height: dialogViewHeight)
        dialogView.backgroundColor = UIColor.white
        dialogView.layer.cornerRadius = 12
        dialogView.layer.borderColor = UIColor.lightGray.cgColor
        dialogView.layer.borderWidth = 0.5
        dialogView.clipsToBounds = true
        
        let separatorLineView2 = UIView()
        separatorLineView2.frame.origin = CGPoint(x: dialogViewWidth/2, y: separatorLineView.frame.origin.y)
        separatorLineView2.frame.size = CGSize(width: 0.5, height: dialogViewHeight - separatorLineView.frame.origin.y - buttonStack.frame.height)
        separatorLineView2.backgroundColor = UIColor.init(white: 0.85, alpha: 1)
        dialogView.addSubview(separatorLineView2)
        
        let buttonSeparatorLineView = UIView()
        buttonSeparatorLineView.frame.origin = CGPoint(x: dialogViewWidth/2, y: buttonStack.frame.origin.y)
        buttonSeparatorLineView.frame.size = CGSize(width: 0.5, height: buttonStack.frame.height)
        buttonSeparatorLineView.backgroundColor = UIColor.init(white: 0.38, alpha: 1)
        dialogView.addSubview(buttonSeparatorLineView)
        
        addSubview(dialogView)
        
        backgroundView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTappedOnBackgroundView)))
    }
    
    @objc func didTappedOnBackgroundView(){
        UIHelper.removeEffect()
        dismiss(animated: true)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func buttonAction(sender :UIButton,event:UIEvent) -> Void {
        let touch  =  (event.allTouches! as NSSet).anyObject() as AnyObject
        let phases :UITouch.Phase = touch.phase
        if phases == UITouch.Phase.began {
            print("按下")
                let btnW = 280/2
            sender.setBackgroundImage(UIImage.imageWithColor(color: UIColor.white, size: CGSize(width: btnW, height: 40)), for:.normal)
            sender.layer.borderWidth = 1
            sender.setTitleColor(UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 1), for: .normal)
        }else if phases == UITouch.Phase.ended{
            print("松开")
            sender.layer.borderWidth = 0
            sender.setBackgroundImage(imageNamed("icon_btn_bg"), for:.normal)
            sender.setTitleColor(UIColor.white, for:  .normal)
        }
    }
}

